import 'dart:convert';

import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter_deer/generated/json/base/json_field.dart';
import 'package:flutter_deer/generated/json/get_merchant_configer.g.dart';
import 'package:flutter_deer/res/constant.dart';
export 'package:flutter_deer/generated/json/get_merchant_configer.g.dart';

@JsonSerializable()
class GetMerchantPreferentialConfigEntity {
  GetMerchantPreferentialConfigEntity();

  factory GetMerchantPreferentialConfigEntity.fromJson(Map<String, dynamic> json) => $GetMerchantPreferentialConfigEntityFromJson(json);
  int rechargeOtherGiftConfig = 0; //充值额外送开关 1开启 2关闭
  int offlineDiscounts = 0; //线下优惠（1：开启，0关闭）
  int roundingMode = 0; //1:保留两位小数,第三位四舍五入  2:保留两位小数, 第三位进位  3:保留两位小数,第三位抹掉

  Map<String, dynamic> toJson() => $GetMerchantPreferentialConfigEntityToJson(this);

  // 保存到sp
  Future<void> save() async {
    await SpUtil.putObject(Constant.merchantConfiger, this);
  }

  // 从sp获取
  static GetMerchantPreferentialConfigEntity? get() {
    final String? jsonString = SpUtil.getString(Constant.merchantConfiger);
    if (jsonString?.isNotEmpty != true) {
      return null;
    }
    try {
      final dynamic data = json.decode(jsonString!);
      if (data is Map<String, dynamic>) {
        return GetMerchantPreferentialConfigEntity.fromJson(data);
      } else {
        print('解析失败：期望 Map<String, dynamic>，但返回的是 ${data.runtimeType}');
        return null;
      }
    } catch (e, stackTrace) {
      print('JSON 解析失败: $e\n$stackTrace');
      return null;
    }
  }
}
